/******************** 
	作用:添加商品
	作者:蔡俊雄
	版本:V1.0
	时间:2015-08-05
	修改:2015-08-06
	修改:2015-08-14(按善强的新原型修改物流运费)
	修改:2015-08-18(添加上架时间和下架时间)
	修改:2015-08-24(商品图片改成可拖动排序)
********************/

$(function() {

	var categoryData = []; //分类数据
	var tableId = "#parameterTable"; //表格ID
	var upShelfUrl = ""; //上架的网址
	var offShelfUrl = ""; //下架的网址
	var deleteUrl = ""; //删除的地址
	var levelTip1 = $("#category1").attr("data-tip"); //一级产品分类提示
	var levelTip2 = $("#category2").attr("data-tip"); //二级产品分类提示
	var idNotSelect = "0"; //不选产品分类时的ID

	/*categoryData = [{
		"id": "1",
		"name": "服装",
		child: [{
			"id": "11",
			"name": "服装1"
		}, {
			"id": "12",
			"name": "服装2"
		}, {
			"id": "13",
			"name": "服装3"
		}]
	}, {
		"id": "2",
		"name": "化妆品",
		child: [{
			"id": "21",
			"name": "化妆品1"
		}, {
			"id": "22",
			"name": "化妆品2"
		}, {
			"id": "23",
			"name": "化妆品3"
		}]
	}, {
		"id": "3",
		"name": "电器",
		child: [{
			"id": "31",
			"name": "电器1"
		}, {
			"id": "32",
			"name": "电器2"
		}, {
			"id": "33",
			"name": "电器3"
		}]
	}, {
		"id": "4",
		"name": "手机",
		child: [{
			"id": "41",
			"name": "手机1"
		}, {
			"id": "42",
			"name": "手机2"
		}, {
			"id": "43",
			"name": "手机3"
		}]
	}];*/
	//	console.log(JSON.stringify(categoryData));
	//添加编辑器
	var kItems=[
						'source','fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
						'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
						'insertunorderedlist', '|', 'emoticons', 'image','multiimage', 'link','videoqq'];
	var kConfigOther={
		items :kItems,
		uploadJson: window.kindeditorUpUrl,
		allowFileManager: false,
		height:600
	};
	var editor = KindEditor.create("#parameter",kConfigOther); //商品参数
	var descriptionEditor = KindEditor.create("#description",kConfigOther); //商品描述
	$('#productTab a:first').tab('show');
	//	$('#productTab a:eq(2)').tab('show');

	//-----------------属性行操作-----------------
	//点击"增加"按钮
	$("#add").on("click", function(e) {
		General.stopEvent(e);
		var str = $("#tpl-parameter").html();
		$(str).appendTo($(tableId)).find("a[data-id=delete]:not([disabled])").on("click", function(e) {
			General.stopEvent(e);
			$(this).closest("tr").remove();
		});
	});
	//点击行内"删除"按钮
	$(tableId + " a[data-id=delete]:not([disabled])").on("click", function(e) {
		General.stopEvent(e);
		$(this).closest("tr").remove();
	});

	//-----------------上传图片设置-----------------
	//编辑器设置
	var uploadEditor = KindEditor.editor(kConfigOther);
	//显示图片上传对话框
	function showImageUploadDialog(imageId, hiddenId) {
		uploadEditor.loadPlugin('image', function() {
			uploadEditor.plugin.imageDialog({
				imageUrl: $(hiddenId).val(),
				clickFn: function(url, title, width, height, border, align) {
					$(hiddenId).val(url);
					$(imageId).attr("src", url);
					uploadEditor.hideDialog();
				}
			});
		});
	}

	function initImageUploadDialog(btnId, imageId, hiddenId) {
		$(btnId).on("click", function(e) {
			showImageUploadDialog(imageId, hiddenId);
		});
	}
	function initImageUploadDel(btnId, imageId, hiddenId){
		$(btnId).on("click", function(e) {
			$(hiddenId).val('');
			$(imageId).attr("src",'');
		});
	}
	//初始化
	function init() {
		var count = $(".image-list li").length; //总共有多少个上传图片
		var btnId, imageId, hiddenId;
		for (var i = 1; i <= count; i++) {
//			btnId = "#previewImage" + i + ",#upload" + i;
			btnId = "#upload" + i;
			imageId = "#previewImage" + i;
			hiddenId = "#imageUrl" + i;
			initImageUploadDialog(btnId, imageId, hiddenId);
			initImageUploadDel("#uploaddel" + i, imageId, hiddenId);
		}
		initCategory(); //初始化一级分类
//		changeFreightTemplate();
		initDatetimePicker(); //初始化选择日期时间
	}

	init(); //初始化
	
	//-----------------拖动图片排序-----------------
	var sort = new Sortable(document.getElementById("imageList"), {
		handle: ".preview-image",
		draggable: "li"
	});
	
	//-----------------物流运费中的运费模板操作-----------------
	$("input[name=freight]").on("click", function(e) {
		//获取当前组别
		var group=$(this).attr("data-group");
		var isCheck=$(this).prop("checked");
		if(isCheck){
			var allFreight=$("input[name=freight]");
			var index=allFreight.index($(this));
			if(group=="fix"){
				allFreight.prop("checked",false);
				$(this).prop("checked",true);
			}else{
				allFreight.filter("[data-group=fix]").prop("checked",false);
			}
		}
	});

	//-----------------选择日期时间-----------------
	//初始化选择日期时间
	function initDatetimePicker() {
		toggleTimeContainer(); //判断是否显示上下架时间
		$("input[name=shelf]").on("click", function(e) {
			toggleTimeContainer(); //判断是否显示上下架时间
		});

		$('#upTime').datetimepicker({
			dayViewHeaderFormat: "YYYY MMMM",
			format: "YYYY-MM-DD HH:mm",
			inline: true,
			sideBySide: true
		});
		$('#downTime').datetimepicker({
			dayViewHeaderFormat: "YYYY MMMM",
			format: "YYYY-MM-DD HH:mm",
			inline: true,
			sideBySide: true
		});
	};
	//判断是否显示上下架时间
	function toggleTimeContainer() {
		var index = $("input[name=shelf]:checked").val();
		if (index === "3") {
			$("#shelfTimeContainer").removeClass("hide")
		} else {
			$("#shelfTimeContainer").addClass("hide");
		}
	};

	//-----------------商品分类关联操作-----------------
	//初始化一级分类
	function initCategory() {
		//获取数据
		try {
			//categoryData=JSON.parse($("#categoryData").val());
			categoryData = eval($("#categoryData").val());
		} catch (e) {
			categoryData = [];
		}

		var str = '';
		str += '<option value="' + idNotSelect + '" selected>' + levelTip1 + '</option>';
		$.each(categoryData, function(index, obj) {
			str += '<option value="' + obj["id"] + '">' + obj["name"] + '</option>';
		});
		$("#category1").html(str);
		changeCategory();
		//根据一级分类切换二级分类
		$("#category1").on("change", function(e) {
			changeCategory();
		});
	};
	//切换分类
	function changeCategory() {
		var id = $("#category1").val();
		var idIndex = -1;
		$.each(categoryData, function(index, obj) {
			if (obj["id"] == id) {
				idIndex = index;
				return false;
			}
		});
		var str = '';
		str += '<option value="' + idNotSelect + '" selected>' + levelTip2 + '</option>';
		if (idIndex != -1) {
			$.each(categoryData[idIndex]["child"], function(index, obj) {
				str += '<option value="' + obj["id"] + '">' + obj["name"] + '</option>';
			});
		}
		$("#category2").html(str);
	}

	//-----------------物流运费中的运费模板操作-----------------
	//根据下拉列表的选项切换运费模板
//	$("#freight").on("change", function(e) {
//		changeFreightTemplate();
//	});
//	//切换运费模板
//	function changeFreightTemplate() {
//		var index = $("#freight option:checked").index();
//		$("#t3 .type-content").removeClass("active").eq(index).addClass("active");
//	}
});